<?php

namespace app\hotel\controller;

use think\Controller;

class Order extends Controller
{
    public function __construct(){
        parent::__construct();

        $this->OrderModel = model('Hotel.Order');

        $this->BusinessModel = model('Business.User');
    }


    // 订单列表
    public function list(){
        if($this->request->isPost()){
            $busid = $this->request->param('busid',0,'trim');
            $status = $this->request->param('status',0,'trim');

            // 查询用户
            $business = $this->BusinessModel->find($busid);
            if(!$business){
                $this->error('用户未知');
                exit;
            }

            $where = [
                'busid' => $busid
            ];

            if($status != 0){
                $where['status'] = $status;
            }

            // 查看订单
            $order = $this->OrderModel
                ->with(['room'])
                ->where($where)
                ->order("id desc")
                ->select();

            if(!$order){
                $this->error('暂无订单信息');
                exit;
            }else{
                $this->success('返回订单',null,$order);
                exit;
            }
        }
    }


    // 订单详情
    public function info(){
        if($this->request->isPost()){
            $id = $this->request->param('id',0,'trim');
            
            // 查询订单
            $order = $this->OrderModel->find($id);
            if(!$order){
                $this->error('订单未知');
                exit;
            }

            // 查询用户
            $business = $this->BusinessModel->find($order['busid']);
            if(!$business){
                $this->error('用户未知');
                exit;
            }

            // 订单数据返回
            $orderinfo = $this->OrderModel
                    ->with(['business','room','coupon'])
                    ->where(['order.id' => $id, 'busid' => $business['id']])
                    ->find();
            
            if(!$orderinfo){
                $this->error('订单数据未知');
                exit;
            }else{
                $this->success('返回订单数据',null,$orderinfo);
                exit;
            }
        }
    }


    // 评价
    public function comment(){
        if($this->request->isPost()){
            $busid = $this->request->param('busid',0,'trim');
            $orderid = $this->request->param('orderid',0,'trim');
            $rate = $this->request->param('rate',0,'trim');
            $comment = $this->request->param('comment','','trim');

            // 查询用户
            $business = $this->BusinessModel->find($busid);
            if(!$business){
                $this->error('用户未知');
                exit;
            }

            // 查询订单
            $order = $this->OrderModel->find($orderid);
            if(!$order){
                $this->error('订单未知');
                exit;
            }

            $data = [
                'id' => $orderid,
                'status' => 4,
                'comment' => $comment,
                'rate' => $rate
            ];

            $result = $this->OrderModel->isUpdate(true)->save($data);

            if($result === FALSE){
                $this->error('评论失败');
                exit;
            }else{
                $this->success('评论成功');
                exit;
            }

        }
    }


    // 退款申请
    public function defund(){
        if($this->request->isPost()){
            $id = $this->request->param('id',0,'trim');
            $defund = $this->request->param('defund','','trim');

            // 查询订单
            $order = $this->OrderModel->find($id);
            if(!$order){
                $this->error('订单未知');
                exit;
            }

            $data = [
                'id' => $id,
                'status' => -1,
                'defund' => $defund
            ];


            $result = $this->OrderModel->isUpdate(true)->save($data);

            if($result === FALSE){
                $this->error('申请失败，请重试');
                exit;
            }else{
                $this->success('申请成功');
                exit;
            }
        }
    }

}
